<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

//私海
class Privatesea extends Backend
{

  // 当前对象模型
  protected $model = null;

  public function _initialize()
  {
    parent::_initialize();
    $this->BusinessModel = model('Business.Business');
    $this->AdminModel = model('Admin');
    $this->ReceiveModel = model('Business.Receive');
    $this->SourceModel = model('Business.Source');

    //性别选项
    $this->genderoption = ["0" => '保密', '1' => '男', '2' => '女'];
    // $this->assign('genderoption', $genderoption);
  }


  public function index()
  {
    if ($this->request->isAjax()) {
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();
      $adminid  = $this->auth->id; //管理员id
      if (array_key_exists('id', $where)) {
        $where['business.id'] = $where['id'];
        unset($where['id']);
      }

      $total = $this->BusinessModel
        ->alias('business')
        ->where('adminid', $adminid)
        ->where($where)
        ->with('source')
        ->order($sort, $order)
        ->count();

      $list = $this->BusinessModel
        ->alias('business')
        ->where('adminid', $adminid)
        ->where($where)
        ->with('source')
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      //！！！（删除下面的）
      /*  $sql = $this->BusinessModel
        ->alias('business')
        ->where('adminid', $adminid)
        ->where($where)
        ->with('source')
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->fetchSql()  // 添加 fetchSql() 方法
        ->select(); */

      $result = array("total" => $total, "rows" => $list);
      // $this->assign('productTypes', $result);
      return json($result);
    }
    return $this->view->fetch();
  }


  //客户详情
 /*  public function detail($ids = null)
  {
    $business = $this->BusinessModel->where('id', $ids)->select();
    var_dump($business);
    exit;
    if (!$business) {
      $this->error('客户不存在');
    }

    // 客户信息
    $info = [
      'nickname' => $business['nickname'], //客户
      'mobile' => $business['mobile'], //客户联系方式
      'email' => $business['email'], //邮箱
      'gender_text' => $business['gender_text'], //性别
      'source_name' => $business['source']['name'], //客户来源
      'deal_text' => $business['deal_text'], //成交状态
      'address' => '', //地区
      'auth_text' => '', //认证状态
      'createtime' => '', //创建时间
      'avatar' => '',
    ];

    $this->assign('row', $info);
    return $this->view->fetch();
  } */



  //客户来源列表
  public function source()
  {

    $sourcelist = $this->SourceModel->select();
    // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
    $list = [];
    foreach ($sourcelist as $key => $value) {
      $list[$value['id']] = $value['name'];
    }
    return $list;
  }


  // 添加
  public function add()
  {
    if ($this->request->isPOST()) {
      $adminid  = $this->auth->id; //管理员id
      $params = $this->request->post("row/a", [], 'trim');
      if ($params['city']) {
        $parts = explode('/', $params['city']);
        $params['province'] = $parts[0] ?? '';
        $params['city'] = $parts[1] ?? '';
        $params['district'] = $parts[2] ?? '';
      }
      $params['adminid'] = $adminid;

      $result = $this->BusinessModel->save($params);
      if (!$result) {
        $this->error($this->model->getError());
      }

      $this->success('添加成功');
    }

    // var_dump($this->source());
    $this->assign([
      'genderoption' => $this->genderoption,
      'authoption' => ['0' => '未认证', '1' => '已认证'],
      'dealoption' => ['0' => '未成交', '1' => '已成交'],
      'sourceoption' => $this->source(),
    ]);
    return $this->view->fetch();
  }


  // 编辑
  public function edit($ids = null)
  {
    $row = $this->BusinessModel->find($ids);
    if (!$row) {
      $this->error('课程不存在');
    }
    $row['region_text2']  = str_replace('-', '/', $row['region_text']);

    if ($this->request->isPOST()) {
      $adminid  = $this->auth->id; //管理员id
      $params = $this->request->post("row/a", [], 'trim');
      if ($params['city']) {
        $parts = explode('/', $params['city']);
        $params['province'] = $parts[0] ?? '';
        $params['city'] = $parts[1] ?? '';
        $params['district'] = $parts[2] ?? '';
      }
      $params['adminid'] = $adminid;

      // halt($params);
      // $result = $this->BusinessModel->save($params);
      $result = $this->BusinessModel->where('id', $ids)->update($params);
      if ($result == false) {
        $this->error('更新失败');
      } else {
        $this->success('更新成功');
      }
    }

    $this->assign([
      'row' => $row,
      'genderoption' => $this->genderoption,
      'authoption' => ['0' => '未认证', '1' => '已认证'],
      'dealoption' => ['0' => '未成交', '1' => '已成交'],
      'sourceoption' => $this->source(),
    ]);

    return $this->view->fetch();
  }

  //回收
  public function recovery($ids = null)
  {
    $adminid  = $this->auth->id; //管理员id
    // halt($ids.'|'.$adminid);
    $result = $this->BusinessModel->where(['id' => $ids, 'adminid' => $adminid])->update(['adminid' => Null]);
    if ($result == false) {
      $this->error('回收失败');
    } else {
      $this->success('回收成功');
    }
  }

  // 删除
  public function del($ids = null)
  {
    $row = $this->BusinessModel->all($ids);

    if (!$row) {
      $this->error('客户不存在');
    }

    $result = $this->BusinessModel->destroy($ids);

    if (!$result) {
      $this->error($this->BusinessModel->getError());
    }

    $this->success();
  }
}
